
module("Block", package.seeall )

function StopWeld( ply )
	
	if ply._WeldingBlock then
		ply._WeldingBlock.Sound:Stop()
		ply._WeldingBlock = nil
		
		umsg.Start("Block", ply )
			umsg.Char( 1 )		
		umsg.End()
	end

end

function WeldThink( ply, tbl )

	local Tbl = ply._WeldingBlock
	local block = Tbl.Block
	
	--[[Check if the block still exists
		The player is alive and still reloading
		And that the block did not change state ]]--
	if !ValidEntity( block ) || !ply:Alive() || !ply:KeyDown( IN_RELOAD ) || Tbl.StartState != block:AllowMotion() then
		return false
	end
	
	local Trace = ply:GetEyeTrace() 
	
	if Trace.Entity != block || !InRange( ply, Trace.HitPos ) then
		return false
	end
	
	local Time = CurTime() - Tbl.StartTime
	
	if Time > GetWeldTime( ply, block, tbl.StartState ) then
		
		block:SetAllowMotion( !tbl.StartState )
		return false
		
	end
	
	if CurTime() >  Tbl.NextSpark then
		
		local effectdata = EffectData()
			effectdata:SetOrigin( Trace.HitPos )
			effectdata:SetNormal( Trace.HitNormal * 2 )
			effectdata:SetMagnitude( 2 )
			effectdata:SetScale( 1 )
			effectdata:SetRadius( 2 )
		util.Effect( "Sparks", effectdata )
		
		Tbl.NextSpark = CurTime() + 0.2
		
	end
	
	return true

end